﻿using System;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using HYElectricDim;
using YRevitJig;
using HYView;

namespace ElectricDim
{
	[Transaction(TransactionMode.Manual)]
	public class CmdCableTrayDim : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			ElectricalUtils.ViewValidating(document);
			DlgCableTrayDim instance = DlgCableTrayDim.GetInstance();
			instance.ShowDialog();
			if (!instance.OK)
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			DlgCableTrayDim.CUserConfig configData = instance.GetConfigData();
			if (!configData.IsValid())
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			Transaction transaction = new Transaction(document, "TransformCv");
			try
			{
				transaction.Start();
				Reference reference = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new CableTrayFilter(), "请选择要标注的桥架");
				Element element = document.GetElement(reference);
				if (element == null)
				{
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				try
				{
					ElementIdDate elementIdDate = Assist.ExtensibleStorageManager.ReadFromElement<ElementIdDate>(element, this.guid);
					if (elementIdDate.GetId().IntegerValue != 0)
					{
						document.Delete(elementIdDate.GetId());
					}
				}
				catch
				{
				}
				ElectricDimJig electricDimJig = new ElectricDimJig(activeUIDocument);
				if (configData.m_DimStyle == LeaderStyle.Orthogonal)
				{
					electricDimJig.ResetPoint = XUiJigBase.EResetPoint.eByOrtho;
				}
				double num = (double)document.ActiveView.Scale;
				electricDimJig.RectWidth = AssistFunc.mmToFeet(10.0) * num;
				electricDimJig.RectHeight = AssistFunc.mmToFeet(5.0) * (double)configData.m_lstItem.Count * num;
				XYZ xyz = electricDimJig.PickDimPoint(reference.GlobalPoint, ElectricDimJig.EDimType.eFourCorner, "请拾取标注点:");
				electricDimJig.Dispose();
				if (xyz == null || xyz.DistanceTo(reference.GlobalPoint) < 0.005)
				{
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Failed;
				}
				Element element2 = element as CableTray;
				XYZ xyz2 = reference.GlobalPoint;
				IntersectionResult intersectionResult = ((element2.Location as LocationCurve).Curve as Line).Project(xyz2);
				xyz2 = new XYZ(intersectionResult.XYZPoint.X, intersectionResult.XYZPoint.Y, xyz2.Z);
				Element element3 = ElectricalUtils.CreateCableTrayDim(activeUIDocument, xyz2, xyz, configData);
				transaction.Commit();
				ElementIdDate obj = new ElementIdDate(element3.Id);
				Assist.ExtensibleStorageManager.WriteToElement(element, this.guid, obj);
			}
			catch (Exception)
			{
				transaction.RollBack();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Guid guid = new Guid("356cda5a-e6c5-4c2f-a9ef-b1223444b8c8");
	}
}
